package com.cskaoyan.service;

import com.cskaoyan.bean.AdminInfoBean;
import com.cskaoyan.bean.InfoData;
import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.mapper.RoleidMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class AuthServiceImp implements AuthService{

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public LoginUserData login(String username, String password,String host) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password,host);
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return null;
        }
        Session session = subject.getSession();
        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();

        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();

        adminInfo.setAvatar(marketAdmin.getAvatar());
        adminInfo.setNickName(marketAdmin.getUsername());
        loginUserData.setAdminInfo(adminInfo);
        //String string = UUID.randomUUID().toString();
        loginUserData.setToken((String) session.getId());
        return loginUserData;
    }

    @Override
    public InfoData info(String token) {
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        Integer[] roleIds = marketAdmin.getRoleIds();
        List<String> pmss = new ArrayList<>();
        List<String> names = new ArrayList<>();
        for (Integer roleId : roleIds) {
            List<String> pms = marketAdminMapper.selectPmsByRoleId(roleId);
            for (String pm : pms) {
                pmss.add(pm);
            }
            String name = marketAdminMapper.selectNameByRoleId(roleId);
            names.add(name);
        }
        for (Integer roleId : roleIds) {
            if (roleId == 1) {
                List<String> m = new ArrayList<>();
                m.add("*");
                InfoData infoData = new InfoData();
                infoData.setAvatar(marketAdmin.getAvatar());
                infoData.setName(marketAdmin.getUsername());
                infoData.setPerms(m);
                infoData.setRoles(names);
                return infoData;
            }
        }
        List<String> apis = new ArrayList<>();

        for (String s : pmss) {
            String api =  marketAdminMapper.selectApisByApiId(s);
            apis.add(api);
        }
        InfoData infoData = new InfoData();
        infoData.setAvatar(marketAdmin.getAvatar());
        infoData.setName(marketAdmin.getUsername());
        infoData.setPerms(apis);
        infoData.setRoles(names);
        return infoData;
    }
}
